package com.hc.appanimal;

import java.util.ArrayList;
import java.util.List;

import com.hc.DTO.AnimalDTO;
import com.hc.DTO.AnimalImagesDTO;
import com.hc.DTO.TokenDTO;
import com.hc.adapter.AnimalImagesAdapter;
import com.hc.common.ActionEvent;
import com.hc.constants.Constants;
import com.hc.controller.AnimalImagesController;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GalleryActivity extends Activity implements OnItemClickListener,
		OnClickListener {
	GridView girdImage;
	ArrayList<Object> arrData;
	AnimalImagesAdapter adapter;
	Button btnMap, btnGallery, btnHome, btnDetail;
	TextView txtTitle;
	Button btnBack;
	AnimalDTO dto;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		txtTitle = (TextView) findViewById(R.id.txtTitle);
		txtTitle.setTextSize(15.0f);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
		Bundle data = getIntent().getExtras();
		if (data != null) {
			dto = (AnimalDTO) data.getSerializable("images");
		}

		txtTitle.setText(dto.getCommonName());
		Bundle dataImg = new Bundle();
		dataImg.putInt("imagesgallary", dto.getId());
		InitData(dataImg);
		girdImage = (GridView) findViewById(R.id.girdImage);
		initImageLoader(getApplicationContext());
		arrData = new ArrayList<Object>();
		adapter = new AnimalImagesAdapter(this, R.layout.grid_row_gallary, arrData);
		girdImage.setAdapter(adapter);
		girdImage.setOnItemClickListener(this);

		btnMap = (Button) findViewById(R.id.btnMap);
		btnGallery = (Button) findViewById(R.id.btnGallery);
		btnMap.setOnClickListener(this);
		btnGallery.setOnClickListener(this);
		btnDetail = (Button) findViewById(R.id.btnDetail);
		btnDetail.setOnClickListener(this);
		btnHome = (Button) findViewById(R.id.btnHome);
		btnHome.setOnClickListener(this);
		
		btnMap.setBackgroundResource(R.drawable.animal_map);
		btnGallery.setBackgroundResource(R.drawable.animal_imagesf);
		btnDetail.setBackgroundResource(R.drawable.animal_details);

	}
	
	
	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.diskCacheSize(50 * 1024 * 1024) // 50 Mb
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	public void InitData(Bundle data) {
		ActionEvent e = new ActionEvent(this, Constants.GALLERY, null, data);
		AnimalImagesController.getInstance().handleViewEvent(e);
	}

	public void handleControllerViewEvent(ActionEvent e) {
		switch (e.action) {
		case Constants.GALLERY:
			@SuppressWarnings("unchecked")
			ArrayList<AnimalImagesDTO> newArr = (ArrayList<AnimalImagesDTO>) e.viewData;
			new VerySlowTask(newArr).execute();
			break;
		default:
			break;
		}
	}

	// sysAnimal
	public class VerySlowTask extends AsyncTask<String, Object, Void> {
		List<AnimalImagesDTO> _newArr;

		public VerySlowTask(List<AnimalImagesDTO> arr) {
			_newArr = arr;
		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				for (int i = 0; i < _newArr.size(); i++) {
					publishProgress(_newArr.get(i));

				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			arrData.add(values[0]);

		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			adapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		try {
			AnimalImagesDTO dto = (AnimalImagesDTO) girdImage
					.getItemAtPosition(position);
			Intent i = new Intent(GalleryActivity.this,
					FullScreenViewActivity.class);
			Bundle data = new Bundle();
			data.putSerializable("arrImg", dto);
			data.putInt("position", position);
			i.putExtras(data);
			startActivity(i);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		Bundle data = null;
		switch (v.getId()) {

		case R.id.btnBack:
			finish();
			break;

		case R.id.btnMap:
			intent = new Intent(GalleryActivity.this, MapActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		case R.id.btnGallery:
			intent = new Intent(GalleryActivity.this, GalleryActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		case R.id.btnHome:
			intent = new Intent(GalleryActivity.this, SummaryActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		case R.id.btnDetail:
			intent = new Intent(GalleryActivity.this,
					DetailAnimalActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}

	}
}
